<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\tool\Result;
use app\common\model\User;
use think\facade\View;
use think\exception\ValidateException;
use think\response\Json;

class Security extends Base
{

    public function index(): Json
    {
        if (!$this->user->pay_password) {
            return json(['code' => -1, 'msg' => '尚未设置支付密码']);
        } else {
            return json(['code' => 1, 'msg' => '已设置支付密码']);
        }
    }

    /**
     * 修改登录/支付密码
     * type: password 登录密码   pay_password 支付密码
     * old_password 原密码
     * password 新密码
     * password_confirm 确认密码
     * @return \think\Response
     */
    public function save()
    {
        $param = $this->request->post('', '', $this->filter);
        if (!$this->user->pay_password) {
            $validate = 'SecurityFirst';
            $first = true;
        } else {
            $validate = 'Security';
            $first = false;
        }
        try {
            $this->validate($param, $validate);
        } catch (ValidateException $e) {
            return Result::fail($e->getMessage());
        }

        $result = User::security($param, $this->user, $first);
        if($result['code'] == 1){
            return Result::success($result['msg']);
        }
        return Result::fail($result['msg']);
    }

}

